package cn.tianjindong.blog_portal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.pojo.BlogArticle;
import cn.tianjindong.blog.pojo.BlogNotice;
import cn.tianjindong.blog_portal.service.NoticeService;

@Controller
public class NoticeController {

	@Autowired
	private NoticeService noticeService;

	/**
	 * 显示公告页面
	 * 
	 * @param model
	 * @param noticeId
	 * @return
	 */
	@RequestMapping("/notice/{noticeId}.html")
	public String toNoticePage(Model model, @PathVariable Long noticeId) {
		BlogNotice notice = noticeService.getNoticeById(noticeId);
		model.addAttribute("notice", notice);
		return "/notice";
	}

	/**
	 * Ajax请求，返回首页右部的公告列表（至多显示四个）
	 * 
	 * @return
	 */
	@RequestMapping("/notice/index-list.action")
	public @ResponseBody String indexNoticeList() {
		Map<String, Object> result = new HashMap<>();
		List<BlogNotice> list = noticeService.getFourNotice();
		result.put("list", list);
		// 获取总公告数
		int count = noticeService.getNoticeCount();
		result.put("count", count);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping("/notice/list.html")
	public String toNoticeListPage(Model model, @RequestParam(defaultValue = "1") Integer currentPage,
			@RequestParam(defaultValue = "10") Integer countItem) {
		PageView<BlogNotice> pageView = noticeService.getNoticePageView(currentPage, countItem);
		model.addAttribute("pageView", pageView);
		return "/notice-list";
	}
}
